package com.iss.efi.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.iss.efi.consts.SystemConst;
import com.iss.efi.dto.ResultDto;
import com.iss.efi.service.EfiWsService;
import com.iss.efi.util.ServiceUtil;
import com.iss.efi.util.SymmetricEncryptionUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.jws.WebService;

@WebService(targetNamespace = "efi.iss.com",
        endpointInterface = "com.iss.efi.service.EfiWsService")
@Component
public class EfiWsServiceImpl implements EfiWsService {

    @Value("${encryption.enable}")
    private Boolean encryptionEnable;


    public String efiIssDataOperation(String params) {

        try {
            String paramsData=params;

            if(encryptionEnable){
                paramsData=SymmetricEncryptionUtil.decrypt(params);
            }

            try {
                JSONObject jsonObject = JSON.parseObject(paramsData);
                String serviceName = jsonObject.getString("serviceName");
                String res= ServiceUtil.getDataHandleService(serviceName).handle(jsonObject.get("data"));
                if(encryptionEnable){
                    return SymmetricEncryptionUtil.encrypt(res);
                }
                return res;
            } catch (Exception e) {
                e.printStackTrace();
                return JSON.toJSONString(new ResultDto(SystemConst.E02));
            }
        } catch (Exception e) {
            return JSON.toJSONString(new ResultDto(SystemConst.E01));
        }
    }
}
